﻿Public Class Login
    Dim dc_resultado As Long
    Dim dg_resultado As String
    Dim dg_usuario As String
    Dim dc_usuario As String
    Dim dg_password As String
    Dim dg_clave_encriptada As String


    Private Function GenerarIngreso() As Boolean
        GenerarIngreso = False
        If Me.TxtPassword.TextLength > 0 And Me.TxtUsuario.TextLength > 0 Then
            dc_usuario = TxtUsuario.Text.Trim.ToUpper
            dg_password = TxtPassword.Text.Trim.ToUpper
            GenerarIngreso = True
        End If
    End Function

    Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click
        If Not GenerarIngreso() Then
            MsgBox("Debe especificar datos de usuario y passwrod", MsgBoxStyle.OkOnly + MsgBoxStyle.Information)
            Exit Sub
        End If

        dc_resultado = busca_usuario_autenticacion(dc_usuario, dg_usuario, dg_password, dg_resultado)

        If dc_resultado <> 0 Then
            MsgBox(dg_resultado, MsgBoxStyle.OkOnly + MsgBoxStyle.Exclamation)
        Else

            dg_clave_encriptada = Encryptar.EncryptString("", TxtPassword.Text.Trim.ToUpper, ModSeguridad.C_dg_clave, Security.Cryptography.CipherMode.ECB)

            If dg_password.Trim.ToUpper <> dg_clave_encriptada.Trim.ToUpper Then
                MsgBox("No se puede autenticar a usuario indicado", MsgBoxStyle.OkOnly + MsgBoxStyle.Exclamation)
                Exit Sub
            End If

            FrmSplash.Show()
            System.Threading.Thread.Sleep(3000)
            FrmSplash.Close()
            MDIPrincipal.Show()
            Me.Close()
        End If

    End Sub

    Private Sub Cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel.Click
        Me.Close()
    End Sub

    Private Sub TxtUsuario_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TxtUsuario.GotFocus
        Me.TxtUsuario.SelectionStart = 0
        Me.TxtUsuario.SelectionLength = Me.TxtUsuario.TextLength
    End Sub

    Private Sub TxtPassword_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TxtPassword.GotFocus
        Me.TxtPassword.SelectionStart = 0
        Me.TxtPassword.SelectionLength = Me.TxtPassword.TextLength
    End Sub

    Private Sub TxtUsuario_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TxtUsuario.KeyPress
        If e.KeyChar = ChrW(Keys.Enter) Then
            e.Handled = True
            SendKeys.Send("{TAB}")
            If GenerarIngreso() Then
                Call OK_Click(sender, e)
            End If
        End If
    End Sub

    Private Sub TxtPassword_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TxtPassword.KeyPress
        If e.KeyChar = ChrW(Keys.Enter) Then
            e.Handled = True
            SendKeys.Send("{TAB}")
            If GenerarIngreso() Then
                Call OK_Click(sender, e)
            End If
        End If
    End Sub

End Class
